package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"video/cache"
	"video/models"
)

func List(c *gin.Context) {
	var video []models.Video
	list := "list"
	key, _ := cache.Rdb.Get(cache.Ctx, list).Result()
	if key != "" {
		models.DB.Find(&video)
		jsonData, _ := json.Marshal(video)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Minute*30)
	} else {
		json.Unmarshal([]byte(key), &video)
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": video,
	})
	return
}
